package cn.lzd.mvp.main.sample;

import android.annotation.SuppressLint;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.OnClick;
import cn.lzd.mvp.R;
import cn.lzd.mvp.base.struct.BaseActivity;
import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.ToastUtils;

import java.util.List;

@SuppressLint("NonConstantResourceId")
public class SampleActivity extends BaseActivity<SamplePresenter> {
    @BindView(R.id.reqResult)
    TextView tvReqResult;
    @BindView(R.id.loadingBG)
    View loadingBG;
    @BindView(R.id.loadingProgress)
    ProgressBar loadingProgress;
    @BindView(R.id.request)
    View request;

    @Override
    public int getContentViewId() {
        return R.layout.activity_sample;
    }

    @Override
    public void initView() {
        super.initView();
        tvReqResult.setMovementMethod(ScrollingMovementMethod.getInstance());
    }

    @OnClick({R.id.request, R.id.loadingBG})
    public void onClick(View view) {
        // 对不同的控件做点击事件处理
        switch (view.getId()) {
            case R.id.request: {
                tvReqResult.setText("");
                view.setEnabled(false);
                mPresenter.reqWeather();
                break;
            }
            case R.id.loadingBG:{
                ToastUtils.showShort("请等待……");
                break;
            }
        }
    }

    void onReceiveWeatherData(List<SampleBeans.DayData> weatherData) {
        StringBuilder stringBuilder = new StringBuilder();
        for (SampleBeans.DayData dayData : weatherData) {
            stringBuilder.append(GsonUtils.toJson(dayData)).append("\n\n");
        }
        tvReqResult.setText(stringBuilder);
        request.setEnabled(true);
    }

    @Override
    public void showLoading() {
        loadingBG.setVisibility(View.VISIBLE);
        loadingProgress.setVisibility(View.VISIBLE);
    }

    @Override
    public void hideLoading() {
        loadingBG.setVisibility(View.GONE);
        loadingProgress.setVisibility(View.GONE);
    }
}
